package com.example.dailyrecord.base.entity;

import com.example.dailyrecord.entity.StatusCode;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class ResultMsg {

    private Object data;
    private String msg;
    private LocalDateTime time;
    private int code;
    private boolean success;

    public static ResultMsg success() {
        return getInstance(StatusCode.SUCCESS);
    }

    public static ResultMsg error() {
        return getInstance(StatusCode.FAIL);
    }

    public static ResultMsg getInstance(StatusCode code) {
        return getInstance(LocalDateTime.now(), code);
    }

    public static ResultMsg getInstance(Object data, StatusCode code) {
        if (code.getCode() >= 400) {
            return getInstance(data, code, false);
        }
        return getInstance(data, code, true);
    }

    private static ResultMsg getInstance(Object data, StatusCode code, boolean success) {
        return getInstance(data, code.getMessage(), code.getCode(), success);
    }

    private static ResultMsg getInstance(Object data, String message, int code, boolean success) {
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setData(data);
        resultMsg.setMsg(message);
        resultMsg.setCode(code);
        resultMsg.setSuccess(success);
        resultMsg.setTime(LocalDateTime.now());
        return resultMsg;
    }
}
